#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>

/*
    leds{
        compatible = "minli,leds";
        led1-gpio = <&gpioe 10 0>;
        led2-gpio = <&gpiof 10 0>;
        led3-gpio = <&gpioe 8 0>;
    }
*/

MODULE_LICENSE("GPL");

typedef struct{
    const char* name;
    struct gpio_desc* pin;
    unsigned int stat;
    struct timer_list timer;
}led_desc_t;

led_desc_t led;

void timer_trigger_func(struct timer_list* timer)
{
    led_desc_t* ledData = container_of(timer, led_desc_t, timer);
    ledData->stat = !ledData->stat;
    gpiod_set_value(ledData->pin, ledData->stat);
    mod_timer(&ledData->timer, jiffies + HZ >> 1);
}

static int __init led_drv_init(void)
{
    struct device_node* ledDevNode;
    ledDevNode = of_find_node_by_path("/leds");
    led.name = "led1";
    led.pin = gpiod_get_from_of_node(ledDevNode, "led1-gpio", 0, GPIOD_OUT_HIGH, led.name);
    led.stat = 1;

    //定时器初始化
    led.timer.expires = jiffies + HZ >> 1;
    timer_setup(&led.timer, timer_trigger_func, 0);
    //启动定时器
    add_timer(&led.timer);
    return 0;
}

static void __exit led_drv_exit(void)
{
    //停止定时器
    del_timer(&led.timer);
    gpiod_put(led.pin);
}

module_init(led_drv_init);
module_exit(led_drv_exit);